#region Using

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using BlogEngine.Core;
using System.Xml;
using System.IO;

#endregion

public partial class widgets_GoogleMaps_edit : WidgetEditBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindList();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //New
        if (string.IsNullOrEmpty(CurrentID))
        {
            XmlDocument doc = Doc();
            XmlNode node = doc.SelectSingleNode("markers");

            if (node == null)
            {
                node = doc.CreateElement("markers");
                doc.AppendChild(node);
            }

            XmlNode link = doc.CreateElement("marker");

            XmlAttribute id = doc.CreateAttribute("id");
            id.InnerText = Guid.NewGuid().ToString();
            link.Attributes.Append(id);

            XmlAttribute title = doc.CreateAttribute("Title");
            title.InnerText = txtTitle.Text.Trim(); ;
            link.Attributes.Append(title);

            XmlAttribute desc = doc.CreateAttribute("Description");
            desc.InnerText = txtDescription.Text.Trim();
            link.Attributes.Append(desc);

            XmlAttribute address = doc.CreateAttribute("Address");
            address.InnerText = txtAddress.Text.Trim();
            link.Attributes.Append(address);

            XmlAttribute Latitude = doc.CreateAttribute("Latitude");
            Latitude.InnerText = txtLatitude.Text.Trim();
            link.Attributes.Append(Latitude);

            XmlAttribute Longitude = doc.CreateAttribute("Longitude");
            Longitude.InnerText = txtLongitude.Text.Trim();
            link.Attributes.Append(Longitude);

            node.AppendChild(link);
            Save(doc);
        }
        //Edit
        else
        {
            XmlDocument doc = Doc();
            XmlNode node = doc.SelectSingleNode("//marker[@id=\"" + CurrentID + "\"]");

            if (node != null)
            {
                node.Attributes["Title"].InnerText = txtTitle.Text;
                node.Attributes["Description"].InnerText = txtDescription.Text;
                node.Attributes["Address"].InnerText = txtAddress.Text;
                node.Attributes["Longitude"].InnerText = txtLongitude.Text;
                node.Attributes["Latitude"].InnerText = txtLatitude.Text;                
                Save(doc);
            }
        }
        BindList();
    }

    private void BindList()
    {
        XmlDocument doc = Doc();
        XmlNodeList list = doc.SelectNodes("//marker");
        if (list.Count > 0)
        {
            using (XmlTextReader reader = new XmlTextReader(doc.OuterXml, XmlNodeType.Document, null))
            {
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.ReadXml(reader);
                grd.DataSource = ds;
                grd.DataBind();
                ds.Dispose();
            }
        }

        txtAddress.Text = txtDescription.Text = txtLatitude.Text = txtLongitude.Text = txtTitle.Text = string.Empty;

        CurrentID = null;
        litHeader.Text = "Add new marker";
        btnAdd.Text = "Add";
    }

    public override void Save()
    {
        XmlDocument doc = Doc();
    }

    void Save(XmlDocument doc)
    {
        StringDictionary settings = GetSettings();
        settings["content"] = doc.InnerXml;
        SaveSettings(settings);
    }

    XmlDocument Doc()
    {
        StringDictionary settings = GetSettings();
        XmlDocument doc = new XmlDocument();
        if (settings["content"] != null)
            doc.InnerXml = settings["content"];

        return doc;
    }

    public string CurrentID 
    { 
        get
        {
            if (ViewState["CurrentGoogleMarkID"] == null)
                return null;
            return (string)ViewState["CurrentGoogleMarkID"];
        } 
        set 
        {
            ViewState["CurrentGoogleMarkID"] = value;
        } 
    }

    protected void grdDeleteRow(object sender, EventArgs e)
    {
        XmlDocument doc = Doc();
        string id = (string)((LinkButton)sender).CommandArgument; // (string)e.CommandArgument;
        XmlNode node = doc.SelectSingleNode("//marker[@id=\"" + id + "\"]");
        if (node != null)
        {
            node.ParentNode.RemoveChild(node);
            Save(doc);
            BindList();
        }
    }

    protected void grdEditRow(object sender, EventArgs e)
    {
        XmlDocument doc = Doc();
        CurrentID = (string)((LinkButton)sender).CommandArgument;
        XmlNode node = doc.SelectSingleNode("//marker[@id=\"" + CurrentID + "\"]");

        if (node != null)
        {
            txtTitle.Text = node.Attributes["Title"].InnerText;
            txtDescription.Text = node.Attributes["Description"].InnerText;
            txtAddress.Text = node.Attributes["Address"].InnerText;
            txtLatitude.Text = node.Attributes["Latitude"].InnerText;
            txtLongitude.Text = node.Attributes["Longitude"].InnerText;

            litHeader.Text = "Change " + txtTitle.Text;
            btnAdd.Text = "Save";
        }
    }
}
